<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-9-10
 * Time: 下午1:13
 */
    class  LoginControl extends CommonControl{
        public function log(){
            //如果session已经存在就提示已经登录
            if(isset($_SESSION['user']))$this->error('你已经登陆');
            //如果不是post提交就提示页面不存在
            if(!IS_POST)$this->error('页面不存在');
            //获得用户名
            $user=trim(adds_html($_POST['account']));
            //判断用户名是否为空
            if(empty($user))$this->error('用户名不能为空');
            //查询用户名是否存在
            $result=M('user')->where('username='."'".$user."'")->find();
            $user1=$result['username'];
            $lock=$result['lock'];
//            var_dump($user);
//            var_dump($user1);
            if($user1!=$user)$this->error('用户不存在');
            $password=md5(trim(adds_html($_POST['pwd'])));
            if(empty($password))$this->error('密码不能为空');
            // $result=M('user')->query("SELECT passwd FROM hd_user WHERE username='$user' AND passwd='$password' LIMIT 1");
            // var_dump($password);
            // var_dump($result);die;
            if($result['passwd']!=$password)$this->error('密码错误');
            if($lock==1)$this->error('对不起，该账户被锁定，无法登陆');
            $data = array(
            'logintime' => time(),
            'loginip'   =>$_SERVER['REMOTE_ADDR'],
                    );
            $re=M('user')->where('uid='.$result['uid'])->update($data);
            if (isset($_POST['auto'])){
                setcookie(session_name(),session_id(),time()+3600*7*24,'/');
            }
            $_SESSION['user'] = $user;
            $_SESSION['uid']=$result['uid'];
            if (isset($_SESSION['url'])) {
                $url=$_SESSION['url'];
                $this->success('登录成功',$url);
            }
            $this->error('登录成功');


        }
            public function out(){
        unset($_SESSION['user']);
        unset($_SESSION['uid']);
        $this->success('退出成功！',__ROOT__);
    }


      
    }